package cn.cque.controller;

import cn.cque.entity.Category;
import cn.cque.entity.PageResult;
import cn.cque.entity.Product;
import cn.cque.service.CategoryService;
import cn.cque.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: XWC
 * @ClassName: ProductController
 * @Date: 2020/10/4
 * @Time: 18:36
 * @Description:
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/detail")
    public ModelAndView findById(String id){
        ModelAndView modelAndView=new ModelAndView();
        //查询数据
        Product product=productService.findById(id);
        modelAndView.addObject("product",product);
        modelAndView.setViewName("/jsp/detail");
        return modelAndView;
    }

    @RequestMapping("/category")
    public ModelAndView findByCid(String cid, Integer currentPage, Integer pageSize){
        ModelAndView mv=new ModelAndView();
        PageResult<Product> pageResult = productService.findByCid(cid, currentPage, pageSize);
        Category category = categoryService.findById(cid);
        mv.addObject("pageResult",pageResult);
        mv.addObject("category",category);
        mv.setViewName("/jsp/list");
        return mv;
    }

    @RequestMapping("/product-category")
    public ModelAndView changePage(String cid, Integer currentPage, Integer pageSize){
        ModelAndView mv = new ModelAndView();
        PageResult<Product> pageResult = productService.findByCid(cid, currentPage, pageSize);
        Category category = categoryService.findById(cid);
        mv.addObject("pageResult",pageResult);
        mv.addObject("category",category);
        mv.setViewName("/jsp/list");
        return mv;
    }
}